/****************************************************************

this file is part of Music Practice Audio Player
Copyright (C)2012 Serhii Ratsyborynskyi (ratz@live.ru)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

******************************************************************/

#include "MPAQtBasePlayer.hpp"

#include <QDebug>


AudioCore::MPAQtBasePlayer::MPAQtBasePlayer(QObject *parent) : QObject( parent ),
    mpPlayerList(), mReadySignals(0), mAbsVolume(100), mChannelList()
{

}

AudioCore::MPAQtBasePlayer::MPAQtBasePlayer(const QStringList &files, QObject *parent):QObject(parent),
    mpPlayerList()
{
    foreach (const QString & file, files)
    {
        QMediaPlayer * player = getPlayer( file );
        player->setParent(this);
        if( player )
        {
            mpPlayerList.push_back( player );
        }
    }
}

void AudioCore::MPAQtBasePlayer::slotSetMedia(QString file)
{
    qDebug() << "Open file" << file;
    QStringList fileList;
    if( QFile(file).exists() )
    {
        AudioCore::MPAFile::decodeMpaFile( QDir::tempPath(), file, fileList);
    }
    mChannelList.clear();
    foreach (const QString & mediaFile, fileList)
    {
        QFileInfo fileNameWrapper(mediaFile);
        mChannelList.push_back(fileNameWrapper.completeBaseName());

        QMediaPlayer * player = getPlayer( mediaFile );
        player->setParent(this);
        if( player )
        {
            mpPlayerList.push_back( player );
        }
    }
}

void AudioCore::MPAQtBasePlayer::slotMute(QList<bool> channels)
{
    if(channels.size() == mpPlayerList.size())
    {
        for(int i = 0; i<mpPlayerList.size(); ++i)
        {
            qDebug() << i+1 << "Mute channel " << channels[i];
            mpPlayerList[i]->setMuted(channels[i]);
        }
    }
}


void AudioCore::MPAQtBasePlayer::slotSetChannelVolume(QList<int> volumeList)
{
    if( volumeList.size() == mpPlayerList.size() )
    {
        for(qint32 i = 0; i < volumeList.size(); ++i )
        {
            mpPlayerList[i]->setVolume(volumeList[i]*mAbsVolume/100);
        }
    }
}

void AudioCore::MPAQtBasePlayer::slotPlay()
{
    qDebug() << "Play";
    foreach (QMediaPlayer * player, mpPlayerList)
    {
        player->play();
    }
}

void AudioCore::MPAQtBasePlayer::slotStop()
{
    qDebug() << "Stop";
    foreach (QMediaPlayer * player, mpPlayerList)
    {
        player->stop();
    }
}

void AudioCore::MPAQtBasePlayer::slotPause()
{
    qDebug() << "Pause";
    foreach (QMediaPlayer * player, mpPlayerList)
    {
        player->pause();
    }
}

void AudioCore::MPAQtBasePlayer::slotVolume(int volume)
{
    qDebug() << "Set volume: " << volume;
    if( volume > 100 )
    {
        mAbsVolume = 100;
    }
    else if( volume < 0 )
    {
        mAbsVolume = 0;
    }
    else
    {
        mAbsVolume = volume;
    }
    QList<int> volumeList;

    foreach (QMediaPlayer * player, mpPlayerList)
    {
        volumeList.push_back(player->volume() * mAbsVolume);
    }

    slotSetChannelVolume(volumeList);
}

void AudioCore::MPAQtBasePlayer::slotStatusChanged(QMediaPlayer::MediaStatus status)
{
    if( status == QMediaPlayer::LoadedMedia  )
    {
        mReadySignals++;

        if( mReadySignals == mpPlayerList.size() )
        {
          qDebug() << "Ready";
          foreach (QMediaPlayer * player, mpPlayerList)
          {
              player->setVolume(80);
          }
          emit signalReady(true);
          emit signalMediaLoaded(mChannelList);
          mReadySignals = 0;
        }
        else
        {
            qDebug() << "Ready media " << mReadySignals << " of " << mpPlayerList.size();
        }
    }
}

QMediaPlayer *AudioCore::MPAQtBasePlayer::getPlayer(const QString &path)
{
    QMediaPlayer * result = 0;
    if( QFile::exists(path) )
    {
       result = new QMediaPlayer(this, QMediaPlayer::LowLatency);
       connect( result, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus) ),
                this, SLOT(slotStatusChanged(QMediaPlayer::MediaStatus) ) );
       result->setMedia( QUrl::fromLocalFile(path) );
    }
    else
    {
        qDebug() << "File does not exists " << path;
    }
    qDebug() <<"Media status: " << result->mediaStatus();

    return result;
}
